package com.alibaba.citrus.cr.mn.order.center.forecastorder.convert;

import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.epoch.app.crforecastsales.bo.ForecastOrderLineBO;
import com.epoch.app.mnordercenter.model.dto.CreateForecastDetailDTO;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class CreateForecastDetailDTOToForecastOrderLineBOConvertor {

    public static final String INTEGER_MAX_VALUE = "2147483647";

    public static ForecastOrderLineBO convert(CreateForecastDetailDTO forecastDetailDTO) {
        ForecastOrderLineBO forecastOrderLineBO = new ForecastOrderLineBO();
        forecastOrderLineBO.setOuterLineId(forecastDetailDTO.getOuterLineId());
        forecastOrderLineBO.setProductCode(forecastDetailDTO.getProductCode());
        forecastOrderLineBO.setQuantity(forecastDetailDTO.getProductQuantity());
        forecastOrderLineBO.setActivityCode(forecastDetailDTO.getActivityId());
        forecastOrderLineBO.setActivityNumber(forecastDetailDTO.getActivityLineNo());
        forecastOrderLineBO.setSaleUnitCode(StringUtils.isNotBlank(forecastDetailDTO.getSalesUnitCode()) ? forecastDetailDTO.getSalesUnitCode() : forecastDetailDTO.getSaleUnitCode());
        forecastOrderLineBO.setDetailRemark(forecastDetailDTO.getNote());
        forecastOrderLineBO.setSourceLineNum(forecastDetailDTO.getSourceLineNum());
        forecastOrderLineBO.setShippingWarehouseCode(forecastDetailDTO.getShippingWarehouseCode());
        forecastOrderLineBO.setDeliveryFactoryCode(forecastDetailDTO.getDeliveryFactoryCode());
        forecastOrderLineBO.setStockCode(forecastDetailDTO.getStockCode());
        forecastOrderLineBO.setBizFeatures(forecastDetailDTO.getBizFeatures());
        // 一盘货新增字段
        forecastOrderLineBO.setGoodsProductationTime(forecastDetailDTO.getProductionDate());
        if (StringUtils.isNotBlank(forecastDetailDTO.getSourcePrice())) {
            AssertUtils.lengthCheck(forecastDetailDTO.getSourcePrice(), 10, false, "OC-01-001-01-15-004", "来单价格");
            AssertUtils.isAmountNumber(forecastDetailDTO.getSourcePrice(), false, "OC-01-001-01-15-006", "来单价格");
            AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(forecastDetailDTO.getSourcePrice())) < 0, "OC-01-001-01-15-006", "来单价格", "最大2147483647");
//            long sourcePrice = new BigDecimal(forecastDetailDTO.getSourcePrice()).multiply(new BigDecimal("100")).longValue();
            BigDecimal sourcePrice = new BigDecimal(forecastDetailDTO.getSourcePrice());
            forecastOrderLineBO.setSourcePrice(sourcePrice);
        }
        if(StringUtils.isNotBlank(forecastDetailDTO.getSourceQuantity())) {
            AssertUtils.lengthCheck(forecastDetailDTO.getSourceQuantity(), 10, false, "OC-01-001-01-15-004", "来单数量");
            AssertUtils.isWholeNumber(forecastDetailDTO.getSourceQuantity(), false, "OC-01-001-01-15-006", "来单数量");
            AssertUtils.isTrue(new BigDecimal(INTEGER_MAX_VALUE).compareTo(new BigDecimal(forecastDetailDTO.getSourceQuantity())) < 0, "OC-01-001-01-15-006", "来单数量", "最大2147483647");
            forecastOrderLineBO.setSourceQuantity(Integer.parseInt(forecastDetailDTO.getSourceQuantity()));
        }
        forecastOrderLineBO.setSourceUnit(forecastDetailDTO.getSourceUnit());
        if (StringUtils.isNotBlank(forecastDetailDTO.getConfirmQuantity())) {
            try {
                forecastOrderLineBO.setConfirmQuantity(Integer.parseInt(forecastDetailDTO.getConfirmQuantity()));
            } catch (Exception e) {
                throw new FunctionException("OC-01-001-01-15-006", "确认数量");
            }
        }
        forecastOrderLineBO.setGiftQuantity(forecastDetailDTO.getGiftQuantity()); // 赠品数量
        forecastOrderLineBO.setTaxRate(forecastDetailDTO.getTaxRate()); // 税率
//        if (StringUtils.isNotBlank(forecastDetailDTO.getActivityTotalAmount())) {
//            try {
//                forecastOrderLineBO.setTotalActivityFee(Long.valueOf(MoneyUtils.moneyStrYuan2Fen(forecastDetailDTO.getActivityTotalAmount())));
//            } catch (Exception e) {
//                throw new FunctionException("OC-01-001-01-15-006", "折扣后总金额");
//            }
//            if (StringUtils.isNotBlank(forecastOrderLineBO.getQuantity())) {
//                AssertUtils.isWholeNumber(forecastOrderLineBO.getQuantity(), true, "OC-01-001-01-15-006", "预报数量");
//                if (Float.parseFloat(forecastOrderLineBO.getQuantity()) <= 0) {
//                    throw new FunctionException("OC-01-001-01-15-006", "预报数量");
//                }
//                try {
//                    int activityPrice = Math.round(forecastOrderLineBO.getTotalActivityFee() / Float.parseFloat(forecastOrderLineBO.getQuantity()));
//                    forecastOrderLineBO.setActivityPrice((long) activityPrice);
//                } catch (Exception e) {
//                    throw new FunctionException("OC-01-001-01-15-006", "折扣后总金额");
//                }
//            }
//        }
        return forecastOrderLineBO;
    }
}
